from UI.UI_customTheme      import Ui_CustomTheme
from PyQt4                  import QtGui
from PyQt4                  import QtCore
from constants.constants    import BACKGROUND_THEME
from constants              import constants
from utils.utils            import getFileFromSystem
from utils.utils            import setTheme
import os

class CustomThemeDialog(Ui_CustomTheme, QtGui.QDialog):
    def __init__(self, configDict={}, parent=None):
        super(CustomThemeDialog,self).__init__(parent)
        self.setupUi(self)
        self.initFields(configDict)
        self.setEvents()
        setTheme(self)
        
    def setEvents(self):
        def setEvent(buttonObj, pathToOpen, refLineEdit, previewLE):
            toExit = (refLineEdit, pathToOpen, previewLE)
            buttonObj.pressed.connect(lambda toExit=toExit: self.launchBrowse(toExit))
        
        setEvent(self.pushButton_base,          self.base,      self.lineEdit_base,     self.label_preview_base)
        setEvent(self.pushButton_buttons,       self.buttons,   self.lineEdit_buttons,  self.label_preview_buttons)
        setEvent(self.pushButton_cancel,        self.cancel,    self.lineEdit_cancel,   self.label_preview_cancel)
        setEvent(self.pushButton_menu,          self.menu,      self.lineEdit_menu,     self.label_preview_menu)
        setEvent(self.pushButton_menubar,       self.menubar,   self.lineEdit_menubar,  self.label_preview_menu_bar)
        setEvent(self.pushButton_ok,            self.okbutt,    self.lineEdit_ok,       self.label_preview_ok)
        setEvent(self.pushButton_tab_widget,    self.tabwidget, self.lineEdit_tabwidget,self.label_preview_tab_widget)
        setEvent(self.pushButton_table,         self.tables,    self.lineEdit_table,    self.label_preview_table)
        self.checkBox_flat.stateChanged.connect(self.setFlat)
        
    def setFlat(self, hide='0'):
        constants.BUTTONS_FLAT = self.checkBox_flat.isChecked()
#         if constants.BUTTONS_FLAT:
#             self.buttonBox.Ok.setFlat(True)
#             self.buttonBox.Cancel.setFlat(True)
#         else:
#             self.buttonBox.Ok.setFlat(True)
#             self.buttonBox.Cancel.setFlat(False)

    def initFields(self, configDict):
        self.setStyleSheet("background-image: url('');")
        if not configDict:
            configDict = BACKGROUND_THEME
        self.configDict = configDict        # {'BACKGROUND_BASE':imagePath, ...}
        self.base       = self.configDict.get('BACKGROUND_BASE', '')
        self.buttons    = self.configDict.get('BACKGROUND_BUTTONS', '')
        self.cancel     = self.configDict.get('BACKGROUND_CANCEL_BUTTONBOX', '')
        self.menu       = self.configDict.get('BACKGROUND_MENU', '')
        self.menubar    = self.configDict.get('BACKGROUND_MENU_BAR', '')
        self.okbutt     = self.configDict.get('BACKGROUND_OK_BUTTONBOX', '')
        self.tables     = self.configDict.get('BACKGROUND_TABLES', '')
        self.tabwidget  = self.configDict.get('BACKGROUND_TABWIDGET', '')
        
        self.lineEdit_base.setText      (self.base)
        self.lineEdit_buttons.setText   (self.buttons)
        self.lineEdit_cancel.setText    (self.cancel)
        self.lineEdit_menu.setText      (self.menu)
        self.lineEdit_menubar.setText   (self.menubar)
        self.lineEdit_ok.setText        (self.okbutt)
        self.lineEdit_table.setText     (self.tables)
        self.lineEdit_tabwidget.setText (self.tabwidget)
        self.checkBox_flat.setChecked(constants.BUTTONS_FLAT)
        
        self.setIcon(self.base,         self.label_preview_base)
        self.setIcon(self.buttons,      self.label_preview_buttons)
        self.setIcon(self.cancel,       self.label_preview_cancel)
        self.setIcon(self.menu,         self.label_preview_menu)
        self.setIcon(self.menubar,      self.label_preview_menu_bar)
        self.setIcon(self.okbutt,       self.label_preview_ok)
        self.setIcon(self.tables,       self.label_preview_table)
        self.setIcon(self.tabwidget,    self.label_preview_tab_widget)
        
    def setIcon(self, iconPath, label):
        if iconPath:
            pixmap = QtGui.QPixmap(iconPath)
            labelHeight = label.height()
            labelWidth  = label.width()
            pixmap = pixmap.scaled(labelWidth, 
                                     labelHeight, 
                                     aspectRatioMode=QtCore.Qt.IgnoreAspectRatio, 
                                     transformMode=QtCore.Qt.FastTransformation)
            label.setPixmap(pixmap)
        
    def launchBrowse(self, localTuple):
        if len(localTuple) == 3:
            refLineEdit, pathToOpen, previewLE = localTuple
            filePath = getFileFromSystem(startPath=pathToOpen)
            if os.path.exists(filePath):
                refLineEdit.setText(filePath)
                self.setIcon(filePath, previewLE)
        